package com.ruicar.afs.cloud.basic.archive.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.archive.condition.ArchiveCondition;
import com.ruicar.afs.cloud.basic.archive.enums.ArchiveEnum;
import com.ruicar.afs.cloud.basic.archive.vo.ArchiveVo;
import com.ruicar.afs.cloud.basic.common.entity.BasicDebitInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.service.BasicDebitInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicRepaymentPlanService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yaorui.zhang
 * @date 2020年8月24日 15:34:42
 */
@Slf4j
@RestController
@RequestMapping("/achive")
@AllArgsConstructor
public class ArchiveController {

    private final BasicMainInfoService basicMainInfoService;
    private final BasicDebitInfoService basicDebitInfoService;
    private final BasicRepaymentPlanService basicRepaymentPlanService;

    @ApiOperation(value = "获取合同结清数据")
    @PostMapping("/getContractEarlySettle")
    public JSONObject getContractInfo(@RequestBody ArchiveCondition condition) {
        try {
            String camainId = condition.getContractId();//合同ID
            if (StringUtils.isEmpty(camainId)) {
                return this.result(0, null, "合同ID不能为空", "合同ID不能为空");
            }
            BasicMainInfo mainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery()
                            .eq(BasicMainInfo::getId, camainId)
                    , false
            );
            if(mainInfo.getCloseDate()!=null){
                ArchiveVo archiveVo = new ArchiveVo();
                archiveVo.setDataId(camainId);
                archiveVo.setStatus(Integer.parseInt(AfsEnumUtil.key(ArchiveEnum.contractClose)));
                archiveVo.setStatusex(Integer.parseInt(AfsEnumUtil.key(ArchiveEnum.contractClose)));
                archiveVo.setCloseDate(DateUtil.format(mainInfo.getCloseDate(), DatePattern.NORM_DATE_PATTERN));
                return this.result(1, JSON.parseObject(JSON.toJSONString(archiveVo)), null, null);
            }else{
                return this.result(0, null, "合同未结清", "合同未结清");
            }
        }catch (Exception e){
            return this.result(0,null,e.toString(),e.toString());
        }
    }

    @ApiOperation(value = "获取放款激活后取消的数据")
    @PostMapping("/getContractLoanCancel")
    public JSONObject getContractLoanCancel(@RequestBody ArchiveCondition condition) {
        try {
            String camainId = condition.getContractId();//合同ID
            if(StringUtils.isEmpty(camainId)){
                return this.result(0,null,"合同ID不能为空","合同ID不能为空");
            }
            BasicMainInfo mainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery()
                            .eq(BasicMainInfo::getId, camainId)
                    ,false
            );
            if(mainInfo!=null){
                ArchiveVo archiveVo = new ArchiveVo();
                archiveVo.setDataId(camainId);
                archiveVo.setStatus(Integer.parseInt(AfsEnumUtil.key(ArchiveEnum.contractCancel)));
                archiveVo.setStatusex(Integer.parseInt(AfsEnumUtil.key(ArchiveEnum.contractCancel)));

                return this.result(1,JSON.parseObject(JSON.toJSONString(archiveVo)),null,null);
            }else{
                return this.result(0, null, "合同不存在", "合同不存在");
            }
        }catch (Exception e){
            return this.result(0,null,e.toString(),e.toString());
        }
    }

    @ApiOperation(value = "获取多品案件逾期次数,逾期天数,逾期状态")
    @PostMapping("/getOverdueInfo")
    public JSONObject getOverdueInfo(@RequestBody ArchiveCondition condition) {
        try {
            String contractNo = condition.getContractNo();//合同ID
            if (StringUtils.isEmpty(contractNo)) {
                return this.result(0, null, "合同编号不能为空", "合同编号不能为空");
            }
            //借据信息表
            BasicDebitInfo basicDebitInfo =  basicDebitInfoService.getOne(Wrappers.<BasicDebitInfo>lambdaQuery()
                            .eq(BasicDebitInfo::getContractNo, contractNo)
                    , false
            );
            //合同还款计划表
            int overdueTimes =  basicRepaymentPlanService.getOverdueInfo(contractNo);
            //拼接数据
            ArchiveVo archiveVo = new ArchiveVo();
            archiveVo.setOverdueTimes(overdueTimes);
            archiveVo.setOverdueDays(basicDebitInfo.getCurrentRelativeOverdue()>0?basicDebitInfo.getCurrentAbsoluteOverdue():0);
            archiveVo.setOverdueStatus(basicDebitInfo.getCurrentRelativeOverdue()>0?21:0);
            return this.result(1, JSON.parseObject(JSON.toJSONString(archiveVo)), null, null);
        }catch (Exception e){
            return this.result(0,null,e.toString(),e.toString());
        }
    }

    /**
     * 调用返回
     * @param success
     * @param data
     * @param message
     * @param code
     * @return
     */
    public JSONObject result(Integer success, JSONObject data, String message, String code) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success",success);
        //成功
        if(1==success){
            jsonObject.put("data",data);
        }else{//失败
            jsonObject.put("message",message);
            jsonObject.put("code",code);
        }
        return jsonObject;
    }

}
